package nothing;

import java.math.BigDecimal;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import nothing.model.Ingredient;
import org.apache.log4j.Logger;

import nothing.dao.ShopperDao;
import nothing.model.Currency;
import nothing.model.Measurement;
import nothing.model.Purchase;
import nothing.model.Shop;

public class Shopper{
    static Logger log = Logger.getLogger(Shopper.class);
    private ShopperDao shopperDao = null;
    
    private Shopper(){};
    
    public Shopper(ShopperDao shopperDao){
        setShopperDao(shopperDao);
    }

    public List<Measurement> getAllMeasurement(){
        return shopperDao.getMeasurementDao().getAll();                
    }
    public Measurement storeMeasurement(Measurement measurement){
        return shopperDao.getMeasurementDao().save(measurement);
    }
    public List<Currency> getAllCurrency(){
        return shopperDao.getCurrencyDao().getAll();                
    }
    public Currency storeCurrency(Currency currency){
        return shopperDao.getCurrencyDao().save(currency);                
    }
    public List<Shop> getAllShops(){
        return shopperDao.getShopDao().getAll();                
    }
    public Shop storeShop(Shop shop){
        return shopperDao.getShopDao().save(shop);                
    }
    public Purchase storePurchase(Purchase purchase){        
        return shopperDao.getPurchaseDao().save(purchase);        
    }        
    public List<Ingredient> generateList(Date from, Date to){
        List ingredientsByDate = shopperDao.getFoodDao().findByDateRange(from,to);
        
        // get ingredients result and consolidate qtys
        Object[] ingredients = Util.listToArray(ingredientsByDate, 2);
        Hashtable<String,Ingredient> shoppingList = new Hashtable<String,Ingredient>();
        for(int i=0;i < ingredients.length;i++){
            //log.debug("...............................................>>"+i+". "+((Ingredient)ingredients[i]).getFood().getName());
            Ingredient ingredient = ((Ingredient)ingredients[i]);
            String name =ingredient.getFood().getName();
            Ingredient storedIngredient = (Ingredient) shoppingList.remove(name);
            if (storedIngredient != null){
                BigDecimal newQty = storedIngredient.getQty().add(ingredient.getQty());
                ingredient.setQty(newQty);
            }    
            shoppingList.put(name, ingredient);
        }
        
        
        final List consolidatedList = new ArrayList<Ingredient>();
        consolidatedList.addAll(shoppingList.values());
        
        // sort by food type for ease of shopping
        Collections.sort(consolidatedList, new IngredientComparator(shopperDao.getFoodTypeDao()));

        return consolidatedList;
    }

    public void setShopperDao(ShopperDao shopperDao) {
        this.shopperDao = shopperDao;
    }
}
